#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/* #define J */

struct tuple {
	char str[21];
	size_t idx;
};

/* {{{ mergesort */
static void
mergesort__merge(void *pbase, size_t nmemb, size_t size,
		int (*cmpf)(const void *, const void *),
		void *pcpybuf,
		size_t L, size_t M, size_t R)
{
	register char *base = (char *) pbase,
		      *cpybuf = (char *) pcpybuf;
	register size_t i = L,
			j = M + 1,
			s = 0; /* index
				  points to where we will place next element */

	while (i <= M || j <= R) {
		if ((*cmpf)((void *) (base + i * size),
					(void *) (base + j * size)) < 0) {
			if (i <= M) {
				memcpy((void *) (cpybuf + s * size),
					(void *) (base + i * size), size);
				s++;
				i++;
			} else if (j <= R) {
				memcpy((void *) (cpybuf + s * size),
					(void *) (base + j * size), size);
				s++;
				j++;
			}
		} else {
			if (j <= R) {
				memcpy((void *) (cpybuf + s * size),
					(void *) (base + j * size), size);
				s++;
				j++;
			} else if (i <= M) {
				memcpy((void *) (cpybuf + s * size),
					(void *) (base + i * size), size);
				s++;
				i++;
			}
		}
	}

	i = 0;
	while (i < s) {
		memcpy((void *) (base + (L + i) * size),
			(void *) (cpybuf + i * size), size);
		++i;
	}
}

static void
mergesort__main(void *base, size_t nmemb, size_t size,
		int (*cmpf)(const void *, const void *),
		void *cpybuf,
		size_t L, size_t R)
{
	size_t M = (L + R) / 2;

	if (L < R) {
		mergesort__main(base, nmemb, size, cmpf, cpybuf, L, M);
		mergesort__main(base, nmemb, size, cmpf, cpybuf, M + 1, R);
		mergesort__merge(base, nmemb, size, cmpf, cpybuf, L, M, R);
	}
}

extern void
mergesort(void *base, size_t nmemb, size_t size,
		int (*cmpf)(const void *, const void *))
{
	void *cpybuf = NULL;

	cpybuf = malloc(nmemb * size + 1UL);
	mergesort__main(base, nmemb, size, cmpf, cpybuf,
			0UL, nmemb - 1UL);
	free(cpybuf);
}
/* }}} */

/* {{{ comparison funcs */
int
cmpstr_lgr(const char *s1, const char *s2)
{
	register int i = 0;

	while (s1[i] && s2[i]) {
		if (s1[i] == s2[i]) {
			i++;
			continue;
		}

		if (tolower(s1[i]) != tolower(s2[i]))
			return tolower(s1[i]) - tolower(s2[i]);
		else
			return s1[i] - s2[i];
	}

	return s1[i] - s2[i];
}

int
isvowel(int c)
{
	if (isalpha(c)) {
		switch (tolower(c)) {
		case 'b': case 'c': case 'd': case 'f': case 'g': case 'h':
		case 'j': case 'k': case 'l': case 'm': case 'n': case 'p':
		case 'q': case 'r': case 's': case 't': case 'v': case 'w':
		case 'x': case 'z':
#ifdef J
			fprintf(stderr, "isvowel: c=%c is consonant\n", (char) c);
#endif
			return 0;
		default:
#ifdef J
			fprintf(stderr, "isvowel: c=%c is vowel\n", (char) c);
#endif
			return 1;
		}
	}
#ifdef J
	fprintf(stderr, "isvowel: c=%c is not a legitimate character\n", (char) c);
#endif
	return 0;
}

int
cmpnames(const void *p1, const void *p2)
{
	const char *s1 = p1, *s2 = p2;
	register size_t i = 0;
	size_t s1_len, s2_len;
	size_t s1_cnsnt = 0UL, s2_cnsnt = 0UL;

	s1_len = strlen(s1);
	s2_len = strlen(s2);

#ifdef J
	fprintf(stderr, "cmpnames: comparing s1=\"%s\" and s2=\"%s\"\n"
			"cmpnames: s1_len=%zu, s2_len=%zu\n",
			s1, s2, s1_len, s2_len);
#endif

	for (i = 0; i < s1_len; i++)
		if (!isvowel(s1[i]))
			s1_cnsnt++;
	for (i = 0; i < s2_len; i++)
		if (!isvowel(s2[i]))
			s2_cnsnt++;
#ifdef J
	fprintf(stderr, "cmpnames: s1_cnsnt=%zu, s2_cnsnt=%zu\n",
			s1_cnsnt, s2_cnsnt);
#endif

	if (s1_cnsnt < s2_cnsnt) {
		return -1;
	} else if (s1_cnsnt > s2_cnsnt) {
		return 1;
	} else {
		return cmpstr_lgr(s1, s2);
	}
}

int
cmpnames_exact(const void *p1, const void *p2)
{
	const char *s1 = p1, *s2 = p2;
	register size_t i = 0;
	size_t s1_len, s2_len;
	size_t s1_cnsnt = 0UL, s2_cnsnt = 0UL;

	s1_len = strlen(s1);
	s2_len = strlen(s2);

#ifdef J
	fprintf(stderr, "cmpnames_exact: comparing s1=\"%s\" and s2=\"%s\"\n"
			"cmpnames_exact: s1_len=%zu, s2_len=%zu\n",
			s1, s2, s1_len, s2_len);
#endif

	for (i = 0; i < s1_len; i++)
		if (!isvowel(s1[i]))
			s1_cnsnt++;
	for (i = 0; i < s2_len; i++)
		if (!isvowel(s2[i]))
			s2_cnsnt++;
#ifdef J
	fprintf(stderr, "cmpnames_exact: s1_cnsnt=%zu, s2_cnsnt=%zu\n",
			s1_cnsnt, s2_cnsnt);
#endif

	if (s1_cnsnt < s2_cnsnt) {
		return -1;
	} else if (s1_cnsnt > s2_cnsnt) {
		return 1;
	} else {
		return cmpstr_lgr(s1, s2);
	}
}
/* }}} */

/* {{{ bsearch */
void *
bsearch(const void *key, const void *pbase, size_t nmemb, size_t size,
		int (*cmpf)(const void *, const void *))
{
	const char *base = (const char *) pbase;
	size_t left = 0,
	       right = nmemb,
	       mid;
	register int cmpres;

	while (left < right) {
		mid = (left + right) / 2;
		cmpres = (*cmpf)(key, (void *) (base + mid * size));
		if (cmpres > 0)
			left = mid + 1;
		else if (cmpres < 0)
			right = mid;
		else
			return (void *) (base + mid * size);
	}

	return NULL;
}
/* }}} */

int
main(int argc, char **argv)
{
	static char name[22];
	static struct tuple all[100000];
	int n = 0, i, m = 0;
	struct tuple *p = NULL;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%20s", name);
		strcpy(all[i].str, name);
		all[i].idx = i + 1;
	}

#ifdef J
	for (i = 0; i < n; i++)
		fprintf(stderr, "main: all[%d] = { .str = \"%s\", .idx = %zd }\n",
				i, all[i].str, all[i].idx);
#endif
	mergesort(all, (size_t) n, sizeof all[0], cmpnames);
#ifdef J
	for (i = 0; i < n; i++)
		fprintf(stderr, "main: all[%d] = { .str = \"%s\", .idx = %zd }\n",
				i, all[i].str, all[i].idx);
#endif

	scanf("%d", &m);
	for (i = 0; i < m; i++) {
		scanf("%20s", name);
#if (defined(J) && 1)
		fprintf(stderr, "main: looking for \"%s\"\n", name);
#endif
		p = bsearch(name, all, (size_t) n, sizeof all[0], cmpnames_exact);
		if (p != NULL) {
#ifdef J
			fprintf(stderr, "main: *p = { .str = \"%s\", .idx = %zd }\n",
					p->str, p->idx);
#endif
			printf("%ld\n", p - &all[0] + 1);
		} else {
			fputs("-1\n", stdout);
		}
	}

	return 0;
}
// vim:foldmethod=marker:
